<template>
  <el-container class="layout-container">
    <el-header class="layout-header">
      <div class="header-left">
        <h1 class="site-title">在线考试系统</h1>
      </div>
      <div class="header-center">
        <el-menu
          :router="true"
          :default-active="route.path"
          mode="horizontal"
          class="horizontal-menu"
        >
          <el-menu-item index="/exam/list">
            <el-icon><Calendar /></el-icon>
            <span>考试列表</span>
          </el-menu-item>
          <el-menu-item index="/score/list">
            <el-icon><Document /></el-icon>
            <span>成绩查询</span>
          </el-menu-item>
          <el-menu-item index="/change-machine">
            <el-icon><Monitor /></el-icon>
            <span>设备更换</span>
          </el-menu-item>
        </el-menu>
      </div>
      <div class="header-right">
        <el-dropdown @command="handleCommand">
          <span class="user-info">
            <el-avatar :size="32" :src="avatarUrl" class="user-avatar"></el-avatar>
            {{ userStore.userInfo?.name ? userStore.userInfo.name[0] + '*' : '' }}
            <el-icon class="el-icon--right"><arrow-down /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">个人信息</el-dropdown-item>
              <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    
    <el-main class="layout-main">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </el-main>

    <el-footer class="layout-footer">
      <div class="footer-content">
        <p>© {{ currentYear }} 在线考试系统 </p>
      </div>
    </el-footer>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ArrowDown, Calendar, Document, Monitor } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/modules/user-student'
import { useExamStore } from '@/stores/modules/exam-student'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const examStore = useExamStore()
// 默认头像
const avatarUrl = ref('https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png')

// 当前年份
const currentYear = computed(() => new Date().getFullYear())

const handleCommand = (command) => {
  if (command === 'profile') {
    router.push('/profile')
  } else if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      userStore.logout()
      examStore.clearAllExamData()
      router.push('/login')
    }).catch(() => {})
  }
}
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  
  .layout-header {
    background-color: #fff;
    border-bottom: 1px solid #dcdfe6;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    
    .site-title {
      margin: 0;
      font-size: 20px;
      color: #409EFF;
      font-weight: bold;
    }
    
    .header-center {
      flex: 1;
      display: flex;
      justify-content: center;
      
      .horizontal-menu {
        border-bottom: none;
      }
    }
    
    .user-info {
      cursor: pointer;
      display: flex;
      align-items: center;
      color: #606266;
      padding: 0 8px;
      border-radius: 4px;
      transition: background-color 0.3s;
      
      &:hover {
        background-color: #f5f7fa;
      }
      
      .user-avatar {
        margin-right: 8px;
      }
      
      .el-icon {
        margin-left: 4px;
      }
    }
  }
  
  .layout-main {
    flex: 1;
    background-color: #f5f7fa;
    padding: 20px;
    overflow-y: auto;
  }
  
  .layout-footer {
    background-color: #fff;
    border-top: 1px solid #dcdfe6;
    padding: 15px 0;
    text-align: center;
    color: #909399;
    font-size: 14px;
    height: auto;
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style> 